//
//  MyOrderDetailViewController.m
//  wtshoppingcite
//
//  Created by TW on 14-12-10.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "MyOrderDetailViewController.h"
#import "MyOrderDetailModel.h"

@interface MyOrderDetailViewController ()
{
    UILabel *titleLabel;
    UILabel *totalLabel;
    UILabel *totalNumLabel;
    UILabel *countNumLabel;
    UILabel *countLabel;
    UILabel *addressLabel;
    UILabel *orderNumLabel;
    UILabel *orderNum;
    
    UILabel *orderName;
    UILabel *orderPhone;

    UILabel *orderTimeLabel;
    UILabel *orderTime;
    UILabel *orderStatusLabel;
    UILabel *orderStatus;
    
    UILabel *shopNameLabel;
    UILabel *shopTel;
}
@end

@implementation MyOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *viewBackGround = [[UIView alloc]initWithFrame:
                              [UIScreen mainScreen].bounds];
    viewBackGround.backgroundColor = UIColorFromRGB(0xdcdddd);
    self.view = viewBackGround;
    
    
    [self setTitleView:@"订单详情"];
    
   [self initWithView];
    
    // Do any additional setup after loading the view.
}

-(void)initWithView{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 110)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, kWidth-20, 40)];
    titleLabel.font = [UIFont systemFontOfSize:16.0f];
    titleLabel.numberOfLines = 0;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = self.myOrderItems.descp;
    [view addSubview:titleLabel];
    
    totalLabel = [[UILabel alloc]initWithFrame:CGRectMake(titleLabel.left, titleLabel.bottom, 35, 22)];
    totalLabel.font = [UIFont systemFontOfSize:15.0f];
    totalLabel.textColor = [UIColor lightGrayColor];
    totalLabel.backgroundColor = [UIColor clearColor];
    totalLabel.text = @"数量:";
    [view addSubview:totalLabel];
    
    
    totalNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(titleLabel.left, titleLabel.bottom, 40, 22)];
    totalNumLabel.right = kWidth-20;
    totalNumLabel.text = self.myOrderItems.count;
    totalNumLabel.textAlignment = NSTextAlignmentRight;
    totalNumLabel.font = [UIFont systemFontOfSize:15.0f];
    totalNumLabel.textColor = [UIColor lightGrayColor];
    totalNumLabel.backgroundColor = [UIColor clearColor];
    [view addSubview:totalNumLabel];
    
    countLabel = [[UILabel alloc]initWithFrame:CGRectMake(totalLabel.left, totalLabel.bottom + 10, 60, 22)];
    countLabel.font = [UIFont systemFontOfSize:15.0f];
    countLabel.textColor = [UIColor lightGrayColor];
    countLabel.backgroundColor = [UIColor clearColor];
    countLabel.text = @"实付款:";
    [view addSubview:countLabel];
    
    
    countNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(totalLabel.left, totalLabel.bottom+ 10, 40, 22)];
    countNumLabel.right = kWidth-20;
    countNumLabel.text = self.myOrderItems.salePrice;
    countNumLabel.textAlignment = NSTextAlignmentRight;
    countNumLabel.font = [UIFont systemFontOfSize:15.0f];
    countNumLabel.textColor = [UIColor lightGrayColor];
    countNumLabel.backgroundColor = [UIColor clearColor];
    [view addSubview:countNumLabel];
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, view.bottom + 10, kWidth, 240)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];

    orderName = [self createLabelWithFrame:CGRectMake(10, 10, 80, 20) text:@"" font:15.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
    [view1 addSubview:orderName];
    
    orderPhone = [self createLabelWithFrame:CGRectMake(orderName.right, 10, 220, 20) text:@"" font:15.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
    [view1 addSubview:orderPhone];
    
    addressLabel = [self createLabelWithFrame:CGRectMake(10, orderName.bottom, 220, 40) text:@"" font:15.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
    [view1 addSubview:addressLabel];
    
    
    orderNumLabel = [self createLabelWithFrame:CGRectMake(addressLabel.left, addressLabel.bottom, 60, 40) text:@"订单号:" font:15.0f textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
    [view1 addSubview:orderNumLabel];
    
    orderNum = [self createLabelWithFrame:CGRectMake(addressLabel.left, addressLabel.bottom, 220, 40) text:@"" font:15.0 textAli:NSTextAlignmentRight textColor:UIColorFromRGB(0x9fa0a0)];
    orderNum.right = kWidth - 15;
    orderNum.text = self.myOrderModel.orderId;
    [view1 addSubview:orderNum];
    
    orderTimeLabel = [self createLabelWithFrame:CGRectMake(orderNumLabel.left, orderNumLabel.bottom, 120, 40) text:@"订单创建时间:" font:15.0f textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
    [view1 addSubview:orderTimeLabel];
    
    orderTime = [self createLabelWithFrame:CGRectMake(orderNumLabel.left, orderNumLabel.bottom, 180, 40) text:@"" font:15.0 textAli:NSTextAlignmentRight textColor:UIColorFromRGB(0x9fa0a0)];
    orderTime.right = kWidth - 15;
    orderTime.text = self.myOrderModel.orderStarted;
    [view1 addSubview:orderTime];
    
    
    orderStatusLabel = [self createLabelWithFrame:CGRectMake(orderTimeLabel.left, orderTimeLabel.bottom, 80, 40) text:@"订单状态:" font:14.0f textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
    [view1 addSubview:orderStatusLabel];
    
    orderStatus = [self createLabelWithFrame:CGRectMake(orderTimeLabel.left, orderTimeLabel.bottom, 60, 40) text:@"" font:15.0 textAli:NSTextAlignmentRight textColor:UIColorFromRGB(0x9fa0a0)];
    orderStatus.right = kWidth - 15;
    
    [view1 addSubview:orderStatus];
    
    shopNameLabel = [self createLabelWithFrame:CGRectMake(orderStatusLabel.left, orderStatusLabel.bottom, 120, 40) text:@"" font:15.0f textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
    [view1 addSubview:shopNameLabel];
    
     _button = [[UIButton alloc]initWithFrame:CGRectMake(orderStatusLabel.left, orderStatusLabel.bottom, 130, 40)];
    [_button setImage:[UIImage imageNamed:@"register_tel"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(telAction) forControlEvents:UIControlEventTouchUpInside];
    [_button setTitleColor:UIColorFromRGB(0x9fa0a0) forState:UIControlStateNormal];
    [_button setFont:[UIFont systemFontOfSize:14.f]];
     _button.right = kWidth - 15;
    [view1 addSubview:_button];
    
    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight-45-64, kWidth, 45)];
    view3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view3];
    
    
    if ([self.myOrderModel.statusId integerValue] == 1){
        orderStatus.text =@"待付款" ;
   
        UIButton *payOrder = [[UIButton alloc]initWithFrame:CGRectMake(0,(45-30)/2,40, 30)];
        payOrder.right = kWidth - 10;
        [payOrder setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [payOrder setFont:[UIFont systemFontOfSize:12.0f]];
        [payOrder setTitle:@"付款" forState:UIControlStateNormal];
        [payOrder setBackgroundImage:[UIImage imageNamed:@"register_select"] forState:UIControlStateNormal];
        [payOrder addTarget:self action:@selector(payOrderAction) forControlEvents:UIControlEventTouchUpInside];
        [view3 addSubview:payOrder];
        

      
        UIButton *cancelOrder = [[UIButton alloc]initWithFrame:CGRectMake(0,(45-30)/2, 68, 30)];
        cancelOrder.right = payOrder.left - 10;
        [cancelOrder setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cancelOrder setFont:[UIFont systemFontOfSize:12.0f]];
        [cancelOrder setTitle:@"取消订单" forState:UIControlStateNormal];
        [cancelOrder setBackgroundImage:[UIImage imageNamed:@"register_select"] forState:UIControlStateNormal];
        [cancelOrder addTarget:self action:@selector(cancelOrderAction) forControlEvents:UIControlEventTouchUpInside];
        [view3 addSubview:cancelOrder];

    }else if ([self.myOrderModel.statusId integerValue] == 4){
        orderStatus.text =@"已付款" ;
        
        UIButton *payOrder = [[UIButton alloc]initWithFrame:CGRectMake(0,(45-30)/2,68, 30)];
        payOrder.clipsToBounds = YES;
        payOrder.layer.cornerRadius = 2;
        payOrder.right = kWidth - 10;
        payOrder.backgroundColor = MAIN_GRAY_COLOR;
        [payOrder setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [payOrder setFont:[UIFont systemFontOfSize:12.0f]];
        [payOrder setTitle:@"待发货" forState:UIControlStateNormal];
       
        [view3 addSubview:payOrder];
        
    }else if ([self.myOrderModel.statusId integerValue] == 8){
        
        orderStatus.text =@"已发货" ;
        UIButton *payOrder = [[UIButton alloc]initWithFrame:CGRectMake(0,(45-30)/2,68, 30)];
        payOrder.right = kWidth - 10;
        [payOrder setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [payOrder setFont:[UIFont systemFontOfSize:12.0f]];
        [payOrder addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
        [payOrder setTitle:@"确认收货" forState:UIControlStateNormal];
        [payOrder setBackgroundImage:[UIImage imageNamed:@"register_select"] forState:UIControlStateNormal];
         [view3 addSubview:payOrder];
        
    }else if ([self.myOrderModel.statusId integerValue] == 32){
        orderStatus.text =@"交易完成" ;
        
        UIButton *payOrder = [[UIButton alloc]initWithFrame:CGRectMake(0,(45-30)/2,68, 30)];
        payOrder.right = kWidth - 10;
        [payOrder setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [payOrder setFont:[UIFont systemFontOfSize:12.0f]];
        [payOrder setTitle:@"评价订单" forState:UIControlStateNormal];
        [payOrder setBackgroundImage:[UIImage imageNamed:@"register_select"] forState:UIControlStateNormal];
        [payOrder addTarget:self action:@selector(commentOrderAction) forControlEvents:UIControlEventTouchUpInside];
        [view3 addSubview:payOrder];
        
        UIButton *cancelOrder = [[UIButton alloc]initWithFrame:CGRectMake(0,(45-30)/2, 68, 30)];
        cancelOrder.right = payOrder.left - 10;
        [cancelOrder setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cancelOrder setFont:[UIFont systemFontOfSize:12.0f]];
        [cancelOrder setTitle:@"删除订单" forState:UIControlStateNormal];
        [cancelOrder setBackgroundImage:[UIImage imageNamed:@"register_select"] forState:UIControlStateNormal];
        [cancelOrder addTarget:self action:@selector(canceledOrderAction) forControlEvents:UIControlEventTouchUpInside];
        [view3 addSubview:cancelOrder];

    }

}


#pragma mark 待付款
-(void)payOrderAction{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(payOrderAction:)]) {
        
        [self.delegate payOrderAction:self.myOrderModel];
    }
}

#pragma mark 取消订单
-(void)cancelOrderAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cancelOrderAction:)]) {
        
        [self.delegate cancelOrderAction:self.myOrderModel];
    }
}

#pragma mark 评价订单
-(void)commentOrderAction{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentOrderAction:)]) {
        
        [self.delegate commentOrderAction:self.myOrderModel];
    }
}

#pragma mark 删除订单
-(void)canceledOrderAction{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(canceledOrderAction:)]) {
        
        [self.delegate canceledOrderAction:self.myOrderModel];
    }
}

#pragma mark 确认收货
-(void)confirmAction{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(confirmAction:)]) {
        
        [self.delegate confirmAction:self.myOrderModel];
    }
}

#pragma mark 打电话
-(void)telAction{
    
    NSString *string =[NSString stringWithFormat:@"tel://%@",self.myOrderDetail.tel] ;
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
}

#pragma mark getMyOrderDetailWithData 订单详情
-(void)getMyOrderDetailWithData:(NSString *)orderId{
    
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    
    SetValueForKey(listDic, orderId, @"orderId");
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kOrderDetail
                             andHttpMethod:@"POST"
                             andParameters:listDic
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         
                         if (response.isError) {
                             DLog(@"error %@",response.errorMessage);
                         }else{
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             
                             NSArray *order = [responseDic objectForKey:@"order"];
                             
                             MyOrderDetailModel *myOrderDetail = [[MyOrderDetailModel alloc]initWithMyOrderDetailInfo:order];
                             self.myOrderDetail = myOrderDetail;
                         }
                        }];
}

-(void)setMyOrderDetail:(MyOrderDetailModel *)myOrderDetail{
    
    if (_myOrderDetail != myOrderDetail) {
        
        _myOrderDetail = myOrderDetail;
      
        orderName.text = myOrderDetail.buyerName;
       // orderPhone.text = ;
        addressLabel.text = myOrderDetail.deliveryAddr;
        shopNameLabel.text = myOrderDetail.shopName;
       // shopTel.text = myOrderDetail.tel;
        [_button setTitle: myOrderDetail.tel forState:UIControlStateNormal];
        
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getMyOrderDetailWithData:self.myOrderModel.orderId];
    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}

//-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
//    if(text == nil)
//        text = @"";
//    UILabel* label = [[UILabel alloc] initWithFrame:frame];
//    label.text = text;
//    label.font = [UIFont systemFontOfSize:font];
//    label.textAlignment = alig;
//    label.textColor = tc;
//    label.backgroundColor = [UIColor clearColor];
//    return label;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
